package com.taoyuanx.utils;


import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;



public final class AesUtil {
static Logger log=LoggerFactory.getLogger(AesUtil.class);
public static final int  DEFAULT_AES_SIZE=256;
	/**
	 * 加密
	 * 
	 * @param content
	 *            需要加密二进制内容
	 * @param password
	 *            加密密码
	 * @param size aes 加密位数
	 * @return
	 */


	public static  byte[] encrypt(byte[] content,String password,Integer size) {
		try {
			if(content==null)return content;
			KeyGenerator kgen = KeyGenerator.getInstance("AES");
			//解决linux异常
			SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG" );  
			secureRandom.setSeed(password.getBytes()); 
			if(null ==size)
				kgen.init(DEFAULT_AES_SIZE, secureRandom);
			else
				kgen.init(size, secureRandom);
			SecretKey secretKey = kgen.generateKey();
			byte[] enCodeFormat = secretKey.getEncoded();
			SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
			Cipher cipher = Cipher.getInstance("AES");// 创建密码器
			cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
			byte[] result = cipher.doFinal(content);
			return result; // 加密
		} catch (Exception e) {
			log.info("加密异常");
		}
		return null;
	}

	
	
	/**
	 * 解密
	 * 
	 * @param content
	 *            解密二进制
	 * @param password
	 *            解密密钥
	 * @return
	 */
	public static byte[] decrypt(byte[] content,String password,Integer size) {
		try {
			if(content==null)return content;
			KeyGenerator kgen = KeyGenerator.getInstance("AES");
			//解决linux异常
			SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG" );  
			secureRandom.setSeed(password.getBytes()); 
			if(null ==size)
				kgen.init(DEFAULT_AES_SIZE, secureRandom);
			else
				kgen.init(size, secureRandom);
			SecretKey secretKey = kgen.generateKey();
			byte[] enCodeFormat = secretKey.getEncoded();
			SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
			Cipher cipher = Cipher.getInstance("AES");// 创建密码器
			cipher.init(Cipher.DECRYPT_MODE, key);// 初始化
			byte[] result = cipher.doFinal(content);
			return result; // 加密
		} catch (Exception e) {
			log.info("解密异常");
		}
		return null;
	}

	// 解密字符串
	public static  String decrypt(String str,String password,Integer size) {
			try {
				if(str==null||"".equals(str))return str;
				KeyGenerator kgen = KeyGenerator.getInstance("AES");
				//根据密码生成随机种子
				SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG" );  
				secureRandom.setSeed(password.getBytes()); 
				if(null ==size)
					kgen.init(DEFAULT_AES_SIZE, secureRandom);
				else
					kgen.init(size, secureRandom);
				SecretKey secretKey = kgen.generateKey();
				byte[] enCodeFormat = secretKey.getEncoded();
				SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
				Cipher cipher = Cipher.getInstance("AES");// 鍒涘缓瀵嗙爜鍣�
				cipher.init(Cipher.DECRYPT_MODE, key);// 鍒濆鍖�
				byte[] result = cipher.doFinal(Base64.decodeBase64(str));
				return new String(result, "UTF-8");
			} catch (Exception e) {
				log.info("解密异常");
				return str;
				
			}
		}
	
	
	// 加密字符串
		public static String encrypt(String str,String password,Integer size) {
			try {
				if(str==null||"".equals(str))return str;
				KeyGenerator kgen = KeyGenerator.getInstance("AES");
				//解决linux异常
				SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG" );  
				secureRandom.setSeed(password.getBytes()); 
				if(null ==size)
					kgen.init(DEFAULT_AES_SIZE, secureRandom);
				else
					kgen.init(size, secureRandom);
				SecretKey secretKey = kgen.generateKey();
				byte[] enCodeFormat = secretKey.getEncoded();
				SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
				Cipher cipher = Cipher.getInstance("AES");// 创建密码器
				cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
				byte[] result = cipher.doFinal(str.getBytes("UTF-8"));
				return Base64.encodeBase64String(result);// 加密
			} catch (Exception e) {
				log.info("加密异常"+str);
				return str;
			
			}
		}




}